import java.awt.event.ActionListener;
import java.awt.Color; 	
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.swing.*;
import javax.swing.border.Border;
import javax.swing.border.EtchedBorder;
import javax.swing.border.LineBorder;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;


public class Menu extends JFrame implements ActionListener{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	private boolean isArmed = false;
	private boolean isPressed = false;
	private boolean isRollover = false;
	private boolean wasArmed = false;
	private boolean wasPressed = false;
	private boolean wasRollover = false;
	
	private JButton button = new JButton("Oh hai, doggie!");
	private JMenuItem quit;
	private JMenuItem blue;
	private JMenuItem red;
	private JMenuItem yellow;
	private JMenuItem etched;
	private JMenuItem lineSizeOne;
	private JMenuItem lineSizeFour;
	private JMenuItem lineSizeSeven;
	private ChangeListener change;
		
	public Menu(){
		initMenu();
	}
	
	@Override
	public void actionPerformed(ActionEvent e) {
		if(quit == e.getSource()){
			System.exit(0);
		}else if(button == e.getSource()){	
			//Button Actions
			Date currentDate = new Date();
			SimpleDateFormat df = new SimpleDateFormat("dd.MM.yyyy 'um' HH:mm:ss");
			System.out.println("Knopf gedrueckt am " + df.format(currentDate));
			System.out.println("------------------------");
			//System.out.println(e.getModifiers());
		
			switch(e.getModifiers()){
			case 17:
				System.out.println("SHIFT-Taste gedrueckt.");
				break;
			case 18:
				System.out.println("CTRL-Taste gedrueckt.");
				break;
			case 19:
				System.out.println("CTRL-Taste und SHIFT-Taste gedrueckt");
				break;
			case 24:
				System.out.println("ALT-Taste gedrueckt.");
				break;
			case 25:
				System.out.println("SHIFT-Taste und ALT-Taste gedrueckt.");
				break;
			case 26:
				System.out.println("CTRL-Taste und ALT-Taste gedrueckt.");
				break;
			case 27:
				System.out.println("CTRL-Taste, SHIFT-Taste und ALT-Taste gedrueckt.");
				break;
			default:
				break;
			}
		}else if(blue == e.getSource()){
			button.setForeground(Color.blue);
		}else if(red == e.getSource()){
			button.setForeground(Color.red);
		}else if(yellow == e.getSource()){
			button.setForeground(Color.yellow);
		}else if(etched == e.getSource()){
			Border etchedBorder = new EtchedBorder(EtchedBorder.RAISED);
			button.setBorder(etchedBorder);
		}else if(lineSizeOne == e.getSource()){
			Border border = new LineBorder(Color.black, 1);
			button.setBorder(border);
		}else if(lineSizeFour == e.getSource()){
			Border border = new LineBorder(Color.black, 4);
			button.setBorder(border);
		}else if(lineSizeSeven == e.getSource()){
			Border border = new LineBorder(Color.black, 7);
			button.setBorder(border);
		}
	}
		
	public void initMenu(){
		//Add Button
		setLayout(new FlowLayout());
		add(button);
		//Configure Button
		button.setFont(new Font("Serif", Font.BOLD, 14));
		//button.setBackground(Color.gray);
		button.setBounds(25, 25, 75, 25);
			
		//Create MenuBar
		JMenuBar menuBar = new JMenuBar();
		
		//Create System Menu
		JMenu systemMenu = new JMenu("System");
		quit = createItem("Programm beenden");
		systemMenu.add(quit);
		menuBar.add(systemMenu);  //Add to MenuBar
		
		//Create Menu for Font Color
		JMenu fontColorMenu = new JMenu("Schriftfarbe");
		blue = createItem("blau");
		fontColorMenu.add(blue);
		red = createItem("rot");
		fontColorMenu.add(red);
		yellow = createItem("gelb");
		fontColorMenu.add(yellow);
		menuBar.add(fontColorMenu);	//Add to MenuBar
		
		//Create Frame Menu
		JMenu frameMenu = new JMenu("Rahmen");
		etched = createItem("Etched");
		frameMenu.add(etched);
		JMenu lineSizeMenu = new JMenu ("Linie mit Liniendicke");
		frameMenu.add(lineSizeMenu);
		lineSizeOne = createItem("1");
		lineSizeMenu.add(lineSizeOne);
		lineSizeFour = createItem("4");
		lineSizeMenu.add(lineSizeFour);
		lineSizeSeven = createItem("7");
		lineSizeMenu.add(lineSizeSeven);
		menuBar.add(frameMenu);	//Add to MenuBar
	
		//Set MenuBar and Layout
		setJMenuBar(menuBar);
		setLocation(800, 100);
		setBounds(50, 50, 300, 90);
		//setBackground(new Color(80000));
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setVisible(true);
		
		//ActionListeners
		button.addActionListener(this);
		change = new ChangeListener() {
			
			@Override
			public void stateChanged(ChangeEvent e) {		
				AbstractButton source = (AbstractButton) e.getSource();
				ButtonModel model = source.getModel();
				
				isArmed = model.isArmed();
				isPressed = model.isPressed();
				isRollover = model.isRollover();
				System.out.println("Armed: " + isArmed + ", Pressed: " + isPressed + ", Rollover: " + isRollover);
				
				if(!isArmed && !isPressed && isRollover && !wasArmed && !wasPressed && !wasRollover) 	
					System.out.println("Maus ueber dem Knopf.");
				
				if(!isArmed && !isPressed && !isRollover && !wasArmed && !wasPressed && wasRollover) 	
					System.out.println("Maus nicht mehr ueber dem Button.");
				
				if(isArmed && isPressed && isRollover && wasArmed && !wasPressed && wasRollover) 		
					System.out.println("Button wurde gedrueckt und Maus ueber dem Button.");
				
				if(!isArmed && isPressed && !isRollover && wasArmed && wasPressed && !wasRollover) 	
					System.out.println("Maus mit gedrueckter Maustaste ausserhalb des Buttons.");
				
				if(isArmed && isPressed && !isRollover && !wasArmed && wasPressed && !wasRollover) 	
					System.out.println("Button mit gedrueckter Maustaste wieder betreten.");
				
				if(!isArmed && !isPressed && !isRollover && !wasArmed && wasPressed && !wasRollover) 	
					System.out.println("Maustaste ausserhalb des gedrueckten Buttons losgelassen.");
				
				if(!isArmed && !isPressed && isRollover && wasArmed) 									
					System.out.println("gedrueckter Button losgelassen.");
				
				if(!isArmed && !isPressed && !isRollover && wasArmed) 									
					System.out.println("gedrueckter Button losgelassen nachdem der Button wieder betreten wurde.");
				
				wasArmed = isArmed;
				wasPressed = isPressed;
				wasRollover = isRollover;
				
			}
		};
		button.addChangeListener(change);
	}
	
	private JMenuItem createItem(String text){
		JMenuItem menuItem = new JMenuItem(text);
		//ActionListener
		menuItem.addActionListener(this);
		return menuItem;
	}
}
